package com.titar.robot.ui.register;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.titar.robot.R;
import com.titar.robot.base.BaseActivity;
import com.titar.robot.entity.LoginResponse;
import com.titar.robot.entity.MemberInfo;
import com.titar.robot.entity.RegistResponse;
import com.titar.robot.entity.VerifyCodeResponse;
import com.titar.robot.ui.main.MainActivity;
import com.titar.robot.utils.SharedPreUtils;
import com.titar.robot.utils.TntUtil;
import com.titar.robot.utils.ToastUtils;
import com.titar.robot.widgets.CountdownTextView;
import com.titar.robot.widgets.DeleteEditText;
import com.titar.robot.widgets.TntToolbar;

public class RegisterActivity extends BaseActivity<RegistPresenter> implements View.OnClickListener, RegisterContact.View {

    DeleteEditText mEtPhone;
    TextView mGetCodeTips;
    DeleteEditText mEtPsw;
    DeleteEditText mEtAgainPsw;
    EditText mEtCardCode;
    CountdownTextView mTvSMS;
    ImageView mBtnRegister;
    ImageView mSendCode;//发送验证码
    ViewFlipper mViewFlipper;//组件间的切换

    private TntToolbar toolbar;
    private String mPhone;
    private String mPsw;
    private String mAgainPsw;
    private boolean isSuccessCode;//判断是否获取验证码界面

    @Override
    protected void onInitWidget(Bundle savedInstanceState) {
        mPresenter = new RegistPresenter().attachView(this);
        getLifecycle().addObserver(mPresenter);
        mEtPhone = findViewById(R.id.et_phone);
        mGetCodeTips = findViewById(R.id.register_code_tips);
        mEtPsw = findViewById(R.id.et_psw);
        mEtAgainPsw = findViewById(R.id.et_psw_again);
        mEtCardCode = findViewById(R.id.et_cardCode);
        mTvSMS = findViewById(R.id.tv_SMS);
        mBtnRegister = findViewById(R.id.btn_register);
        mSendCode = findViewById(R.id.register_send_code);
        mViewFlipper = findViewById(R.id.register_flipper);

        toolbar = (TntToolbar) findViewById(R.id.toolbar);
        toolbar.getIvLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isSuccessCode) {
                    exitCode();
                } else
                    finish();
            }
        });
        toolbar.getTitle().setText(getString(R.string.register));
        if (getIntent() != null) {

            String phone = getIntent().getStringExtra("phone");
            if (phone != null) {
                mEtPhone.setText(phone);
                mEtPhone.setSelection(phone.length());
            }
        }
    }

    @Override
    protected void onInitListener() {
        mTvSMS.setOnClickListener(this);
        mBtnRegister.setOnClickListener(this);
        mSendCode.setOnClickListener(this);
    }

    private void exitCode() {
        showDialog(getString(R.string.reminder), "验证码短信可能略有延迟，确定返回并重新开始！", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewFlipper.showPrevious();//显示输入密码界面
                toolbar.getTitle().setText("注册");
                isSuccessCode = false;
                mTvSMS.reset();
            }
        }, null);

    }

    @Override
    public int getContentViewId() {
        return R.layout.layout_register_activity;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && isSuccessCode) {
            exitCode();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_SMS:
                checkVerifyCode();
                //重新获取验证码
                //   mPresenter.getVerifyCode(mPhone);
                break;
            case R.id.btn_register:
                onClickRegister();
                break;
            case R.id.register_send_code:
                //发送验证码
                //  checkVerifyCode();
                mTvSMS.setBeforeText("重新获取");
                mTvSMS.callOnClick();
                break;

        }
    }

    private void checkVerifyCode() {

        mPhone = mEtPhone.getText().toString().trim();
        mPsw = mEtPsw.getText().toString();
        mAgainPsw = mEtAgainPsw.getText().toString().trim();
        if (TextUtils.isEmpty(mPhone)) { //号码 为空判断
            ToastUtils.showToast(R.string.phone_can_not_null);
            mTvSMS.reset();
            return;
        }
     /*   if (!TntUtil.isMobile(mPhone)) {
            showToast(getString(R.string.phone_number_format_error));
            mTvSMS.reset();
            return;
        }*/

        if (TextUtils.isEmpty(mPsw)) {
            ToastUtils.showToast(getString(R.string.psw_can_not_null));
            mTvSMS.reset();
            return;
        }

        /*if (!TntUtil.isLigalPsw(mPsw)) {
            showToast(getString(R.string.password_format_error_hint));
            mTvSMS.reset();
            return;
        }*/

        if (!TntUtil.isNewLigalPsw(mPsw)) {
            ToastUtils.showToast(R.string.password_format_error_longnum);
            mTvSMS.reset();
            return;
        }
        if (!mPsw.equals(mAgainPsw)) {
            ToastUtils.showToast(getString(R.string.password_format_error_nosame));
            mTvSMS.reset();
            return;
        }

        mPresenter.getVerifyCode(mPhone);
    }


    private void onClickRegister() {
        mPhone = mEtPhone.getText().toString().trim();
        mPsw = mEtPsw.getText().toString().trim();
        mAgainPsw = mEtAgainPsw.getText().toString().trim();
        String cardCode = mEtCardCode.getText().toString();

        if (TextUtils.isEmpty(cardCode)) {  //验证码 为空判断
            ToastUtils.showToast(getString(R.string.verification_code_cannot_be_empty));
            return;
        } else if (TextUtils.isEmpty(mPhone)) { //号码 为空判断
            ToastUtils.showToast(getString(R.string.phone_can_not_null));
            return;
        } else if (TextUtils.isEmpty(mPsw)) {   //密码 为空判断
            ToastUtils.showToast(getString(R.string.psw_can_not_null));
            return;
        } /*else if (!TntUtil.isMobile(mPhone)) {  //号码格式 合法性判断
            showToast(getString(R.string.phone_number_format_error));
            return;
        } */ else if (!TntUtil.isNewLigalPsw(mPsw)) {
            ToastUtils.showToast(getString(R.string.password_format_error_longnum));
            mTvSMS.reset();
            return;
        } else if (!mPsw.equals(mAgainPsw)) {
            ToastUtils.showToast(getString(R.string.password_format_error_nosame));
            mTvSMS.reset();
            return;
        }

        //注册处理
        mPresenter.regist(mPhone, mPsw, cardCode);
    }


    @Override
    public void onGetVerifyCodeSuccess(VerifyCodeResponse response) {
        if (isSuccessCode) {//如果在填写验证码界面
            return;
        }
        mViewFlipper.showNext();//条状到获取验证码界面
        toolbar.getTitle().setText("手机验证码");
        isSuccessCode = true;
        mGetCodeTips.setText("短信验证码已发送到 86 " + mEtPhone.getText() + "，请填写验证码：");
    }

    @Override
    public void onGetVerifyCodeFailed() {
        mTvSMS.reset();
    }

    @Override
    public void onRegistSucess(RegistResponse response) {

        //注册成功，自动登录
        mPresenter.login(mPhone, mPsw);
    }

    @Override
    public void onLoginSuccess(LoginResponse bean) {
        //登录成功，保存token
        SharedPreUtils.getInstence(RegisterActivity.this).setToken(bean.getToken());

        //获取成员数据
        mPresenter.getMemberInfo(bean.getToken());
    }

    @Override
    public void onGetMemberInfoSuccess(MemberInfo memberInfo) {
        //获取成员数据成功,跳转到主界面
        jump2ActivityAndFinish(MainActivity.class);
    }
}
